/* 
  1.项目中所有请求由该文件发出
  2.以后每当发请求之前，都要在该文件里添加一个方法
*/

import myAxios from './myAxios'
import jsonp from 'jsonp'
// import {
//   message
// } from 'antd'


//登录请求
export const reqLogin = ({
  username,
  password
}) => (myAxios.post(`/login`, {
  username,
  password
}))

//登录分类请求
export const reqCategoryList = () => (myAxios('/manage/category/list'))


//添加分类请求
export const reqCategoryAdd = categoryName => (myAxios.post('/manage/category/add', categoryName))

//更新分类请求
export const reqCategoryUpdate = (categoryId, categoryName) => (myAxios.post('/manage/category/update', {
  categoryId,
  categoryName
}))

//获取商品分页列表
export const reqProductList = (pageNum, pageSize) => (myAxios('/manage/product/list', {
  params: {
    pageNum,
    pageSize
  }
}))

// 对商品进行上架 / 下架处理
export const reqProductUpdateStatus = (productId, status) => (myAxios.post('/manage/product/updateStatus', {
  productId,
  status
}))

// 根据Name/desc搜索产品分页列表
export const reqProductSearchList = (pageNum, pageSize, searchType, keyWord) => (myAxios('/manage/product/search', {
  params: {
    pageNum,
    pageSize,
    [searchType]: keyWord
  }
}))

//  根据商品ID获取商品
export const reqProductById = productId => (myAxios('/manage/product/info', {
  params: {
    productId
  }
}))


// 根据分类ID获取分类
export const reqCategoryById = categoryId => (myAxios('/manage/category/info', {
  params: {
    categoryId
  }
}))


// 删除上传的图片
export const reqDeletePicture = name => (myAxios.post('/manage/img/delete', {
  name
}))

// 添加商品
export const reqAddProduct = (productObj) => (myAxios.post('/manage/product/add', productObj))

// 获取角色列表
export const reqRoleList = () => (myAxios('/manage/role/list'))


//  添加角色
export const reqAddRole = (roleNameObj) => (myAxios.post('/manage/role/add', roleNameObj))


//  添加角色
export const reqUpdateRole = (authObj) => (myAxios.post('/manage/role/update', authObj))

// 获取所有用户列表
export const reqUserList = () => (myAxios('/manage/user/list'))


//  添加用户
export const reqAddUser = (userObj) => (myAxios.post('/manage/user/add', userObj))

//  更新修改用户
export const reqUpdateUser = (userObj) => (myAxios.post('/manage/user/update', userObj))

// 删除用户
export const reqDeleteUser = (userId) => (myAxios.post('/manage/user/delete', {
  userId
}))








// 获取天气信息接口  （网上找的）跨源读取阻塞，响应被阻止了。
//#region 
export const reqWeather = () => {
  return new Promise((resolve, reject) => {
    jsonp(`http://api.k780.com/?app=weather.today&weaId=59&appkey=10003&sign=b59bc3ef6191eb9f747dd4e83c99f2a4&format=json`, {
        param: 'jsoncallback'
      },
      (err, data) => {
        if (err) reject()
        else {
          resolve(data)
        }
      })
  })
}